/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 * 						Sébastien Dufay
 * 						Romain Noel
 * 						Nicolas Dennewald
 * 						Slim Aloui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.esiee.mobiliteam.handler;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.esiee.mobiliteam.data.RSSFeed;
import org.esiee.mobiliteam.data.RSSItem;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Classe s'occupant du parsing du flux RSS le répartissant dans les
 * différentes catégories.
 * @author Marleix Mathieu
 * @author Dufay Sébastien
 * @author Romain Noel
 * @author Nicolas Dennewald
 * @author Slim Aloui
 */
public class RSSHandler extends DefaultHandler {

	RSSFeed rssFeed;
	RSSItem rssItem;
	String lastElementName = "";
	final int RSS_TITLE = 1;
	final int RSS_LINK = 2;
	final int RSS_DESCRIPTION = 3;
	final int RSS_CATEGORY = 4;
	final int RSS_PUBDATE = 5;
	int currentstate = 0;

	public RSSHandler()
	{
	}
	public RSSFeed getFeed()
	{
		return rssFeed;
	}
	public void startDocument() throws SAXException
	{
		rssFeed = new RSSFeed();
		rssItem = new RSSItem();

	}
	public void endDocument() throws SAXException
	{
	}
	public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
	{
		if (localName.equals("channel"))
		{
			currentstate = 0;
			return;
		}
		if (localName.equals("item"))
		{
			rssItem = new RSSItem();
			return;
		}
		if (localName.equals("title"))
		{
			currentstate = RSS_TITLE;
			return;
		}
		if (localName.equals("description"))
		{
			currentstate = RSS_DESCRIPTION;
			return;
		}
		if (localName.equals("link"))
		{
			currentstate = RSS_LINK;
			return;
		}
		if (localName.equals("category"))
		{
			currentstate = RSS_CATEGORY;
			return;
		}
		if (localName.equals("pubDate"))
		{
			currentstate = RSS_PUBDATE;
			return;
		}
		
		currentstate = 0;
	}
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException
	{

		if (localName.equals("item"))
		{
			rssFeed.addItem(rssItem);
			return;
			
		}
	}
	public void characters(char ch[], int start, int length)
	{
		String theString = new String(ch,start,length);
		
		switch (currentstate)
		{
			case RSS_TITLE:
				rssItem.setTitle(theString);
				currentstate = 0;
				break;
			case RSS_LINK:
				rssItem.setLink(theString);
				currentstate = 0;
				break;
			case RSS_DESCRIPTION:
				rssItem.setDescription(theString);
				rssItem.setDate(matchedDate("([0-9]{1,2}/[0-9]{1,2}/[0-9]{4,4})",theString) + " " + matchedDate("([0-9]{1,2}h[0-9]{1,2}) ",theString).replace('h',':'));
				rssItem.setPeriode(matchedDate("[0-9]{1,2}h[0-9]{1,2} - ([0-9]{1,2}h[0-9]{1,2})",theString));
				currentstate = 0;
				break;
			case RSS_CATEGORY:
				rssItem.setCategory(theString);
				currentstate = 0;
				break;
			case RSS_PUBDATE:
				rssItem.setPubDate(theString);
				currentstate = 0;
				break;
			default:
				return;
		}
	}
	/**
	 * Fonction de match quasiment identique à celle contenue dans
	 * ActivityShowDescription sans l'objet.
	 * @param expression
	 * @param source
	 * @return
	 */
	public static String matchedDate(String expression,String source){
		String objet = "";
		Pattern p=Pattern.compile(expression);
		Matcher m=p.matcher(source);
		while(m.find()) 
			objet += m.group(1);
		return objet;
	}
}
